import com.webservice.dto.ResponseCommonForm;
import com.webservice.service.RmGetDataApiService;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class MyWebserviceTest {

    public static ExecutorService es  = new ThreadPoolExecutor(100, 200, 20L, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(), Executors.defaultThreadFactory());

    //供应商信息
    @Test
    public void testSup() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_PO_VENDOR_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("STARTTIME", "200001010001");
        map.put("ENDTIME", "209912310001");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        int count = 0;
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) {
                if(result.get(i).getOU_ID().equals("81")){
                    System.out.println(result.get(i).supToString());
                    count++;
                }
            }
        else System.out.println("请求返回信息为空");
        System.out.println("总数量:"+ count);
    }

    //客户信息
    @Test
    public void testCust() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_OM_CUSTOMER_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("START_DATE", "200001010001"); /*参数为START_DATE*/
        map.put("END_DATE", "209912310001");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        System.out.println("总数量:"+ result.size());
        if (result != null && result.size() > 0){
            for (int i = 0; i < result.size(); i++)
                if(result.get(i).getOU_ID().equals("81")) {
                    System.out.println(result.get(i).custToString());
            }
        }
        else System.out.println("请求返回信息为空");
    }

    //物料信息
    @Test
    public void testMtrl() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_INV_ITEM_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("START_DATE", "190001010001"); /*参数为START_DATE*/
       // map.put("END_DATE", "202105080001");
        map.put("ITEM_NUM", "1122041000042");

        map.put("ORG_ID", "102");
        map.put("TO_ROW", "2");//必须大于等于2不然拉取不到
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).mtrlToString());
        else System.out.println("请求返回信息为空");
        //System.out.println("总数量:"+ result.size());
    }

    //仓库信息
    @Test
    public void testStore() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_INV_STORE_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("STARTTIME", "200001010001");
        map.put("ENDTIME", "209912310001");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).storeToString());
        else System.out.println("请求返回信息为空");
        System.out.println("总数量:"+ result.size());
    }

    //部门信息
    @Test
    public void testDept() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_INV_DEPT_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("STARTTIME", "200001010001");
        map.put("ENDTIME", "209912310001");
        //map.put("ORG_ID", "104");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).deptToString());
        else System.out.println("请求返回信息为空");
        System.out.println("总数量:"+ result.size());
    }

    //账户别名信息
    @Test
    public void testAlias() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_INV_ALIAS_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("STARTTIME", "201804270002");
        map.put("ENDTIME", "201805270002");
        map.put("ORG_ID", "102");
        map.put("DEPT_ID", "825004");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).aliasToString());
        else System.out.println("请求返回信息为空");
    }

    //ERP工单信息(请求时长较久)
    @Test
    public void testOrder() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_WIP_ORDER_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("START_DATE", "202101250001");
        map.put("END_DATE", "202102010001");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).orderToString());
        else System.out.println("请求返回信息为空");
        System.out.println("总数量:"+ result.size());
    }

    //成品发货通知单信息
    @Test
    public void testShip() throws IOException, DocumentException { RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();String docType = "CUX_OM_SHIPMENT_Q";Map<String, String> map = new HashMap<String, String>();map.put("STARTTIME", "201805270002");map.put("ENDTIME", "201805280002");map.put("ORG_ID", "102");List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);if (result != null && result.size() > 0) for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).shipToString());else System.out.println("请求返回信息为空"); }

    /*PO*/
    @Test
    public void testPo() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_PO_ORDER_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("PO_NO", "101210101408");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).poToString());
        else System.out.println("请求返回信息为空");
    }

    //ASN
    @Test
    public void testAsn() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_PO_ASN_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("ASN_NO", "ASN-501861-2103-026");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).asnToString());
        else System.out.println("请求返回信息为空");
        System.out.println(result.size());
    }

    //手动获取ERP工单
    @Test
    public void testHandOrder() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_WIP_ORDER_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("ERP_MO", "WM21050175");
        //map.put("ERP_MO", "KZ19050071");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).handOrderToString());
        else System.out.println("请求返回信息为空");
    }

    //BOM(报错: 参数ERP_MO值不能为空。)
    @Test
    public void testBom() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_WIP_BOM_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("PROD_ID", "4022000100042");
        //5020028100108
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0) {
            for (int i = 0; i < result.size(); i++)
            System.out.println(result.get(i).bomToString());
        }else {
            System.out.println("请求返回信息为空");
        }
        System.out.println("总数量:"+ result.size());
    }

    @Test
    public void testThread() {
        for (int i = 0; i< 3; i++){
            es.execute(() -> {
                //System.out.println(new Date());
                System.out.println("1");
                System.out.println("2");
            });
        }
    }

    @Test
    public void testThreadBom() throws InterruptedException {
        int a = 50000;
        CountDownLatch countDownLatch = new CountDownLatch(a);
        for (int i = 0; i< a; i++){
            es.execute(() -> {
                try {
                    testBom();
                    countDownLatch.countDown();
                }catch (Exception e){
                    countDownLatch.countDown();
                    e.printStackTrace();
                }
            });
        }
        countDownLatch.await();
        System.out.println("结束");
    }

    //erp工单物料
    @Test
    public void testOrderMtrl() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_WIP_ITEM_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("ERP_MO", "\n" +
                "WP22030063");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).erpOrderMtrlToString());
        else System.out.println("请求返回信息为空");
    }

    //erp库存对比查询
    @Test
    public void testErpStore() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_INV_OHHAND_Q";
        Map<String, String> map = new HashMap<String, String>();
        //map.put("STORE_ID", "JZ_0311");
        map.put("MTRL_ID", "1430011100029");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).erpStoreCompareToString());
        else System.out.println("请求返回信息为空");
        System.out.println(result.size());
    }

    //客退品 RMA
    @Test
    public void testRma() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_OM_RMA_Q";
        Map<String, String> map = new HashMap<String, String>();
//        map.put("SO_NO", "JZ_0101");
//        map.put("CUST_ID", "29113");
//        map.put("MTRL_ID", "29113");
        map.put("START_DATE", "202004010001");
        map.put("END_DATE", "202005010001");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).RMAtoString());
        else System.out.println("请求返回信息为空");
    }


    //成品出货
    @Test
    public void testChengPin() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_OM_SHIPMENT_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("STARTTIME", "202105190001");
        //map.put("ENDTIME", "202105200001");
        map.put("SHIP_NO","CH220330048");
        map.put("ORG_ID", "102");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).deptToString());
        else System.out.println("请求返回信息为空");
        System.out.println("总数量:"+ result.size());
    }


    //修改ERP单据状态
    @Test
    public void testModifyMoStatus() throws IOException, DocumentException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_MES_CHECK_PARAMTER";
        Map<String, String> map = new HashMap<String, String>();
        map.put("BILL_TYPE", "WIP");
        //map.put("ENDTIME", "202105200001");
        map.put("BILL_ID","481");
        map.put("BILL_NUMBER", "WM1710911123");
        map.put("ACTION_FLG", "Y");
        map.put("CREATE_BY", "SUPER_ADMIN");
        map.put("LAST_UPDATE_BY", "SUPER_ADMIN");
        map.put("STATUS_CODE", "未下达");
        List<ResponseCommonForm> result = rmGetDataApiService.getDate(docType, map);
        if (result != null && result.size() > 0)
            for (int i = 0; i < result.size(); i++) System.out.println(result.get(i).deptToString());
        else System.out.println("请求返回信息为空");
        System.out.println("总数量:"+ result.size());
    }



}

